<?php

namespace Home\Service;

use Home\DAO\DrillTagDAO;
use Home\DAO\DrillCategoryDAO;

/**
 * 题库Service
 *
 * @author 李静波
 */
class DrillService extends PSIBaseExService {
	private $LOG_CATEGORY_DRILL_TAG = "习题标签";

	/**
	 * 返回所有的标签
	 */
	public function tagList($params) {
		if ($this->isNotOnline()) {
			return $this->emptyResult();
		}
		
		$dao = new DrillTagDAO($this->db());
		
		return $dao->tagList($params);
	}

	/**
	 * 新增或编辑习题标签
	 *
	 * @param array $params        	
	 * @return array
	 */
	public function editDrillTag($params) {
		if ($this->isNotOnline()) {
			return $this->notOnlineError();
		}
		
		$db = $this->db();
		
		$id = $params["id"];
		
		$params["dataOrg"] = $this->getLoginUserDataOrg();
		
		$db->startTrans();
		
		$dao = new DrillTagDAO($db);
		
		$rc = null;
		$log = null;
		if ($id) {
			$rc = $dao->updateDrillTag($params);
			$log = "编辑习题标签:" . $params["name"];
		} else {
			$rc = $dao->addDrillTag($params);
			$log = "新增习题标签:" . $params["name"];
		}
		
		if ($rc) {
			$db->rollback();
			return $rc;
		}
		
		// 记录业务日志
		
		$bs = new BizlogService($db);
		$bs->insertBizlog($log, $this->LOG_CATEGORY_DRILL_TAG);
		
		$db->commit();
		
		$id = $params["id"];
		
		return $this->ok($id);
	}

	public function tagInfo($params) {
		if ($this->isNotOnline()) {
			return $this->emptyResult();
		}
		
		$dao = new DrillTagDAO($this->db());
		return $dao->tagInfo($params["id"]);
	}

	public function drillCategoryList() {
		if ($this->isNotOnline()) {
			return $this->emptyResult();
		}
		
		$params = [
				"loginUserId" => $this->getLoginUserId()
		];
		
		$dao = new DrillCategoryDAO($this->db());
		return $dao->drillCategoryList($params);
	}
	
	public function drillList($params){
		if ($this->isNotOnline()) {
			return $this->emptyResult();
		}
		
		return $this->emptyResult();
	}
}